package com.qf.main.pharmacy.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.common.db.utils.CommonBeanutils;
import com.qf.main.pharmacy.common.qo.ShopQo;
import com.qf.main.pharmacy.common.vo.PharmacyVO;
import com.qf.main.pharmacy.common.vo.ShopVo;
import com.qf.main.pharmacy.entity.Pharmacy;
import com.qf.main.pharmacy.entity.Shop;
import com.qf.main.pharmacy.mapper.PharmacyMapper;
import com.qf.main.pharmacy.mapper.ShopMapper;
import com.qf.main.pharmacy.service.ShopService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author chengqiang
 * @Create 2024-02-19-10:36
 * @Description:
 */
@Service
public class ShopServiceImpl implements ShopService {
    @Resource
    private ShopMapper shopMapper;
    @Resource
    private PharmacyMapper pharmacyMapper;

    @Override
    public Integer saveShop(ShopQo shopQo){
        Shop shop = CommonBeanutils.convertTo(shopQo, Shop::new);
        Snowflake snowflake = new Snowflake();
        Long id = snowflake.nextId();
        shop.setShopNo(id);

        return shopMapper.insert(shop);
    }
    @Override
    public Integer removeShop(String shopNo){
        QueryWrapper<Shop> qw = new QueryWrapper<>();
        qw.eq(Shop.COL_SHOP_NO,Long.valueOf(shopNo));

        return shopMapper.delete(qw);
    }

    @Override
    public Integer modifyShop(ShopQo shopQo) {
        Shop shop = CommonBeanutils.convertTo(shopQo, Shop::new);
        QueryWrapper<Shop> qw = new QueryWrapper<>();
        qw.eq(Shop.COL_SHOP_NO, shopQo.getShopNo());
        return shopMapper.update(shop,qw);
    }

    @Override
    public ShopVo getShop(String shopNo) {
        QueryWrapper<Shop> qw = new QueryWrapper<>();
        qw.eq(Shop.COL_SHOP_NO, shopNo);
        Shop shop = shopMapper.selectOne(qw);

        return  CommonBeanutils.convertTo(shop, ShopVo::new);
    }

    @Override
    public PageInfo<PharmacyVO> mallListByShopNo(int pageNum,int pageSize,  String shopNo) {
        QueryWrapper<Pharmacy> qw = new QueryWrapper<>();
        qw.eq(Pharmacy.COL_PHARMACY_SHOP_NO, shopNo);
        PageInfo<PharmacyVO> PageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(
                () -> CommonBeanutils.convertTo(pharmacyMapper.selectList(qw),PharmacyVO::new));

        return PageInfo;
    }

    @Override
    public Integer status(String shopNo,int status) {

        QueryWrapper<Shop> qw = new QueryWrapper<>();
        qw.eq(Shop.COL_SHOP_NO,Long.valueOf(shopNo));
        Shop shop = new Shop();
        shop.setShopStatus(status);

        return shopMapper.update(shop,qw);
    }


}
